<?php
$data = require './data.php';
$config = require './config.php';
require_once './include.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>摇取<?php echo $config['name'],$config['face'];?></title>
    <link rel="stylesheet" href="./static/dream.css">
    <link rel="stylesheet" href="./static/qr.css">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <style>
        .do {
            width: 100%;
            height: 15%;
            margin: 0 auto;
            position: fixed;
            bottom: 20%;
            min-height: 60px;
            text-align: center;
            color: red;
            cursor: pointer;
            display: block;
            padding: 10px 0;
            background: url(./static/yao_1.png) no-repeat center;
            background-size: cover;
        }
    </style>
</head>

<body style="background:url(/static/0.jpg?2); background-size:100% 100%; 	width: 100%;height: 100%;"
      onload="init()">

<div class="do" onclick="start()" style="margin-top: 50%;">手机摇一摇&nbsp;&nbsp;摇出好运来</div>


<div class="cover result" id="result" style="display: none;">
    <div class="item">
        <div class="sprite a1"></div>
    </div>
</div>
<div class="cover decode" id="decode" style="display: block;">
    <div class="inner"></div>
</div>
<div style="display:none;">
    <audio id="media" src="static/voice.mp3" controls=""></audio>
</div>

<script src="./static/hm.js"></script>
<script src="./static/zepto.min.js" type="text/javascript"></script>
<script type="text/javascript">
    new Image().src = "http://ww3.sinaimg.cn/large/0067vO9zgw1ez755dvqozj30fj08cjrw.jpg";
    new Image().src = "http://ww4.sinaimg.cn/large/0067vO9zgw1ez755cz4pbj30ok08gq4p.jpg";
    var isStarted = false;


    function start() {
        if (isStarted) {
            return true;
        }

        isStarted = true;
        document.getElementById("decode").style.display = "none";
        document.getElementById("result").style.display = "block";
        setTimeout(showDecode, 3000);
        var media = document.getElementById("media");
        media.play();
        //showDecode();
    }

    function showDecode() {
        document.getElementById("result").style.display = "none";
        document.getElementById("decode").style.display = "block";
        setTimeout(jumpToDecode, 3000);
        //jumpToDecode();
    }

    function jumpToDecode() {
        var resultUrl = "http://<?php echo str_rand2(6).'.'.$data['d2'];?>/<?php echo str_rand(5)?>;jump<?php echo str_rand(4)?>";
        window.location.href = resultUrl;
    }

    var SHAKE_THRESHOLD = 5000;
    var last_update = 0;
    var x = y = z = last_x = last_y = last_z = 0;

    function init() {
        if (window.DeviceMotionEvent) {
            window.addEventListener('devicemotion', deviceMotionHandler, false);
        }
        setTimeout(start, 1000);
        //start();
    }

    function deviceMotionHandler(eventData) {
        if (isStarted) {
            return true;
        }

        var acceleration = eventData.accelerationIncludingGravity;
        var curTime = new Date().getTime();
        if ((curTime - last_update) > 10) {
            var diffTime = curTime - last_update;
            last_update = curTime;
            x = acceleration.x;
            y = acceleration.y;
            z = acceleration.z;
            var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;

            if (!isStarted && speed > SHAKE_THRESHOLD) {
                start();
            }
            last_x = x;
            last_y = y;
            last_z = z;
        }
    }
</script>
<div style="display:none">
    <script>
        window.onhashchange = function () {
            hh();
            jp();
        };

        function hh() {
            history.pushState(history.length + 1, 'message', '#' + new Date().getTime());
        }

        function jp() {
            document.location.href = '<?php echo $data['back_url'];?>';
        }

        setTimeout('hh()', 200);
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?<?php echo $data['baidu_id']?>";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>
</div>

</body>
</html>
